package com.bsoft.gol.hcs.specialist.service.impl;

import com.bsoft.gol.hcs.clinical.bean.req.QueryInspectionItemDtailReq;
import com.bsoft.gol.hcs.clinical.bean.req.QueryInspectionItemListReq;
import com.bsoft.gol.hcs.clinical.bean.response.QueryInspectionItemDtailResp;
import com.bsoft.gol.hcs.clinical.bean.response.QueryInspectionItemListResp;
import com.bsoft.gol.hcs.clinical.dao.ExamineRequisitionDetailDAO;
import com.bsoft.gol.hcs.clinical.dao.ExamineRequisitionRecordDAO;
import com.bsoft.gol.hcs.clinical.dao.OdsDiagnosisDAO;
import com.bsoft.gol.hcs.enumcase.PlatformEnum;
import ctd.spring.boot.annotation.SsdevService;
import hcn.service.commonClinical.qo.SysParameterRequest;
import com.bsoft.gol.hcs.common.bean.response.ParameterResponse;
import com.bsoft.gol.hcs.common.service.ICommonService;
import ctd.controller.exception.ControllerException;
import ctd.util.annotation.RpcService;
import com.bsoft.gol.hcs.enumcase.SysParameterEnum;
import com.bsoft.gol.hcs.service.rpc.IBaseParameterService;
import com.bsoft.gol.hcs.his.service.IInspectionHisService;
import com.bsoft.gol.hcs.his.service.IInspectionHisXMLService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.bsoft.gol.hcs.specialist.service.IExamineService;
import com.bsoft.gol.hcs.utils.CommonUtils;
import com.bsoft.gol.hcs.utils.Constants;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@SsdevService("specialistExamineService")
public class ExamineService implements IExamineService {
    private static final Logger logger = LoggerFactory.getLogger(ExamineService.class);
    @Autowired
    ExamineRequisitionRecordDAO examineRequisitionRecordDAO;
    @Autowired
    ExamineRequisitionDetailDAO examineRequisitionDetailDAO;
    @Autowired
    OdsDiagnosisDAO odsDiagnosisDAO;
    @Autowired
    IInspectionHisService inspectionHisService;
    @Autowired
    IInspectionHisXMLService inspectionHisXMLService;
    @Autowired
    private ICommonService commonService;
    @Autowired
    private IBaseParameterService baseParameterService;
    @Value("${project_source}")
    private String projectSource;
    /**
     /**
     * 获取检验单项目列表
     */
    @Override
    @RpcService
    public List<QueryInspectionItemListResp> queryInspectionItemList(QueryInspectionItemListReq queryInspectionItemListReq) throws ControllerException {
       List<QueryInspectionItemListResp> baseResponse = new ArrayList<>();

        if (CommonUtils.isNull(queryInspectionItemListReq.getOrganizationId()) && CommonUtils.isNotNull(queryInspectionItemListReq.getHospitalCode())) {

            if (Constants.GOL.equals(projectSource)) {
                queryInspectionItemListReq.setOrganizationId(queryInspectionItemListReq.getHospitalCode());
            } else {
                SysParameterRequest req = new SysParameterRequest();
                req.setType(projectSource);
                List<String> strings = new ArrayList<>();
                strings.add(SysParameterEnum.CM_MULTI_HOSPITAL_CODE.getParameterKey());
                req.setParameterKeyList(strings);
                List<ParameterResponse> list = baseParameterService.getSysParameterList(req).getData();
                if (CommonUtils.isNotEmpty(list)) {
                    queryInspectionItemListReq.setOrganizationId(list.get(0).getParameterValue());
                } else {
                    throw new ControllerException("未配置系统参数：多院区业务数据机构识别");
                }
            }
        }

        String type = this.getType(queryInspectionItemListReq.getOrganizationId());//对接类型
        //0json 1xml
        if ("0".equals(type)) {
            baseResponse = inspectionHisService.queryInspectionItemList(queryInspectionItemListReq).getData();
        } else if ("1".equals(type)) {
            baseResponse = inspectionHisXMLService.queryInspectionItemListXML(queryInspectionItemListReq).getData();
        }

        return baseResponse;
    }

    private String getType(String orgId) {
        String type = "";
        ParameterResponse parameterResponse =
                commonService.getParameterResponse(PlatformEnum.Format_Interface_exchange.name(), orgId);
        if (CommonUtils.isNotEmpty(parameterResponse)) {
            type = parameterResponse.getParameterValue();
        }
        return type;
    }


    /**
     * 2、获取检验申请单子项目明细
     *
     * @param queryInspectionItemDtailReq
     * @return
     */
    @Override
    @RpcService
    public List<QueryInspectionItemDtailResp> queryInspectionItemDtail(QueryInspectionItemDtailReq queryInspectionItemDtailReq) throws ControllerException {
        String type = this.getType(queryInspectionItemDtailReq.getOrganizationId());//对接类型
       List<QueryInspectionItemDtailResp> baseResponse = new ArrayList<>();
        //0json 1xml
        if ("0".equals(type)) {
            baseResponse = inspectionHisService.queryInspectionItemDtail(queryInspectionItemDtailReq).getData();
        } else if ("1".equals(type)) {
            baseResponse = inspectionHisXMLService.queryInspectionItemDtailXML(queryInspectionItemDtailReq).getData();
        }

        return baseResponse;
    }

}
